package com.rachel.activitytest.activity;

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.AbsListView;

import com.google.android.exoplayer2.SimpleExoPlayer;
import com.rachel.activitytest.R;
import com.rachel.activitytest.listview.LBaseAdapter;
import com.rachel.activitytest.listview.LBaseViewholder;
import com.rachel.activitytest.video.CusTomListView;
import com.rachel.activitytest.video.CusVideoListener;
import com.rachel.activitytest.video.CusVideoView;

import java.util.ArrayList;
import java.util.List;

public class ExoPlayerActivity extends BaseActivity implements AbsListView.OnScrollListener {
    private static final String TAG = "moon";
    private CusVideoView mCusVideoView;
    private SimpleExoPlayer mPlayer;
    private int mCurrentPosition = -1;
    private CusTomListView mListview;
    private View mItemView;
    private int mLandscalePosition = -1;
    private int[] resid = new int[]{
            R.mipmap.beauty,R.mipmap.beauty2,R.mipmap.beuty1,R.mipmap.image,
            R.mipmap.image2,R.mipmap.image7,R.mipmap.sea,R.mipmap.loading
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cusvideo_layout);
        mListview = (CusTomListView) findViewById(R.id.listview);
        List<String> mStrings = new ArrayList<>();
        mStrings.add("http://baobab.kaiyanapp.com/api/v1/playUrl?vid=47277&editionType=default&source=qcloud");
        mStrings.add("http://gslb.miaopai.com/stream/ed5HCfnhovu3tyIQAiv60Q__.mp4");
        mStrings.add("http://gslb.miaopai.com/stream/ed5HCfnhovu3tyIQAiv60Q__.mp4");
        mStrings.add("http://gslb.miaopai.com/stream/ed5HCfnhovu3tyIQAiv60Q__.mp4");
        mStrings.add("http://gslb.miaopai.com/stream/ed5HCfnhovu3tyIQAiv60Q__.mp4");
        mStrings.add("http://gslb.miaopai.com/stream/ed5HCfnhovu3tyIQAiv60Q__.mp4");
        mStrings.add("http://gslb.miaopai.com/stream/ed5HCfnhovu3tyIQAiv60Q__.mp4");
        mStrings.add("http://gslb.miaopai.com/stream/ed5HCfnhovu3tyIQAiv60Q__.mp4");

        LBaseAdapter adapter = new LBaseAdapter<String>(this,mStrings,R.layout.listview_video){


            @Override
            public void convert(final LBaseViewholder viewholder, String data, final int position) {
                CusVideoView cusVideoView = viewholder.getView(R.id.customvideo);
                cusVideoView.setTag("listview scroll "+position);
                cusVideoView.setSourceUri(ExoPlayerActivity.this,data, new CusVideoListener() {
                    @Override
                    public void setLandscaleOrPortrait(int derection) {
                        mCurrentPosition = position;

                        if (derection == Configuration.ORIENTATION_PORTRAIT){
                            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                        }else{
                            mLandscalePosition = mListview.getFirstVisiblePosition();
                            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

                        }
                    }
                });
                cusVideoView.setInfoCount(position*20+ " 次播放");
                cusVideoView.setinfoText("万万没想到，客服竟然是这样的，以后打电话要小心了");
                cusVideoView.setPrevieImge(resid[position]);
            }


        };
        mListview.setAdapter(adapter);
        mListview.setOnScrollListener(this);


    }



    @Override
    public void onBackPressed() {
        super.onBackPressed();

    }

    @Override
    protected void onPause() {
        super.onPause();


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }



    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
            //切换到横屏状态
            /*if (Build.VERSION.SDK_INT >= 19) {
                View decorView = getWindow().getDecorView();
                decorView.setSystemUiVisibility(
                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                                | View.SYSTEM_UI_FLAG_FULLSCREEN
                                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
            }*/
            WindowManager.LayoutParams attrs = getWindow().getAttributes();
            attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
            getWindow().setAttributes(attrs);
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
            ActionBar actionBar = getSupportActionBar();
            actionBar.hide();
            mListview.post(new Runnable() {
                @Override
                public void run() {
                    mListview.requestFocusFromTouch();
                    mListview.setSelection(mCurrentPosition);
                }
            });
        }else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){

            WindowManager.LayoutParams attrs = getWindow().getAttributes();
            attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().setAttributes(attrs);
            getWindow().clearFlags(
                    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

            ActionBar actionBar = getSupportActionBar();
            actionBar.show();
            mListview.post(new Runnable() {
                @Override
                public void run() {
                    mListview.requestFocusFromTouch();
                    mListview.setSelection(mLandscalePosition);
                }
            });
        }

    }


    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        Log.d(TAG, "onScroll() called with: , firstVisibleItem = " +
                "[" + firstVisibleItem + "], visibleItemCount = [" + visibleItemCount + "]," +
                " totalItemCount = [" + totalItemCount + "]");
    }
}
